package com.mtons.mblog.web.controller.admin;


//public class FaceLoginController {
//
//
//}

import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.modules.data.AccountProfile;
import com.mtons.mblog.modules.entity.User;
import com.mtons.mblog.modules.entity.UserOauth;
import com.mtons.mblog.modules.repository.UserOauthRepository;
import com.mtons.mblog.modules.repository.UserRepository;
import com.mtons.mblog.web.controller.BaseController;
import com.mtons.mblog.web.controller.site.Views;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mtons.mblog.web.controller.site.Views;

@Controller
@RequestMapping("/faceLogin")
public class FaceLoginController extends BaseController {



    @Autowired
    UserRepository userRepository;


    @RequestMapping("/searchFace")
    @ResponseBody
    public String searchFace(String img, String username) {

        String view = view(Views.LOGIN);

        JSONObject js = FaceSpot.searchFace(img, "face", username);

        User s =userRepository.findByUsername(username);

        if(s==null){
            js.put("id","");
        }else {
            js.put("id",s.getId()+"");

            System.out.println(js.toString(2));
        }

        return js.toString();


    }





    @RequestMapping("/addFace")
    @ResponseBody
    public String addFace(String img, String username) {
        JSONObject js = FaceSpot.addFace(img,"face",username);

        System.out.println(js.toString(2));

        return js.toString();

    }




    /**
     * 提交登录
     * @param username
     * @param password
     * @param model
     * @return
     */
    @PostMapping(value = "/loginValidate")
    public String login(String username,
                        String password,
                        @RequestParam(value = "rememberMe",defaultValue = "0") Boolean rememberMe,
                        ModelMap model) {
        String view = view(Views.LOGIN);

        Result<AccountProfile> result = executeLogin(username, password, rememberMe);

        if (result.isOk()) {
            view = String.format(Views.REDIRECT_USER_HOME, result.getData().getId());
        } else {
            model.put("message", result.getMessage());
        }
        return view;
    }


    @RequestMapping("login")
    public String test(){
        return "/admin/face/login";
    }

}